package cn.stylefeng.guns.modular.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.entity.IdjAuthentication;
import cn.stylefeng.guns.modular.entity.IdjCompany;
import cn.stylefeng.guns.modular.entity.IdjUser;
import cn.stylefeng.guns.modular.mapper.IdjAuthenticationMapper;
import cn.stylefeng.guns.modular.mapper.IdjCompanyMapper;
import cn.stylefeng.guns.modular.mapper.IdjUserMapper;
import cn.stylefeng.guns.modular.service.IIdjUserService;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author TANGWJ
 * @since 2020-07-24
 */
@Service
public class IdjUserServiceImpl extends ServiceImpl<IdjUserMapper, IdjUser> implements IIdjUserService {

	@Autowired
	private IdjUserMapper mapper;
	
	@Autowired
	private IdjCompanyMapper companyMapper;
	
	@Autowired
	private IdjAuthenticationMapper authenticationMapper;
	
	@Override
	public LayuiPageInfo getList(Page<IdjUser> page, String userName, Integer personal, String company, String beginTime, String endTime) {
        Page<Map<String, Object>> list = mapper.getList(page, userName, personal, company, beginTime, endTime);
        list.getRecords().forEach(f -> {
        	if(ToolUtil.isNotEmpty(f.get("user_id"))){
        		IdjCompany idjCompany = companyMapper.selectByKeyId(new Long(f.get("user_id").toString()));
        		if(idjCompany != null){
        			if(ToolUtil.isNotEmpty(idjCompany.getCompanyName())){
        				f.put("company_name", idjCompany.getCompanyName());
        			}
        		}
        		IdjAuthentication authentication = authenticationMapper.selectByUserId(new Long(f.get("user_id").toString()));
        		if(authentication != null){
        			if(ToolUtil.isNotEmpty(authentication.getStatus())){
        				f.put("status", authentication.getStatus());
        			}
        		}
        	}
        	if(ToolUtil.isNotEmpty(f.get("user_identity"))){
        		 String user_identity = f.get("user_identity").toString();
            	 String[] str = user_identity.split(",");
            	 String identity = "";
            	 for (int i = 0; i < str.length; i++) {
    				if(str[i].equals("0")){
    					if (ToolUtil.isNotEmpty(identity)) {
    						identity += "/普通用户";
    					}else{
    						identity += "普通用户";
    					}
    				}
    				if(str[i].equals("1")){
    					if (ToolUtil.isNotEmpty(identity)) {
    						identity += "/艺人";
    					}else{
    						identity += "艺人";
    					}
    				}
    				if(str[i].equals("2")){
    					if (ToolUtil.isNotEmpty(identity)) {
    						identity += "/公司";
    					}else{
    						identity += "公司";
    					}
    				}
    				if(str[i].equals("3")){
    					if (ToolUtil.isNotEmpty(identity)) {
    						identity += "/经纪人";
    					}else{
    						identity += "经纪人";
    					}
    				}
    			}
            	 f.put("identityName", identity);
        	}else{
        		f.put("identityName", "普通用户");
        	}
        });
		return LayuiPageFactory.createPageInfo(list);
	}

	@Override
	public ModelAndView edit(Long userId) {
		IdjUser idjUser = mapper.selectByUserId(userId);
		if(idjUser != null){
			if(ToolUtil.isNotEmpty(idjUser.getServiceType())){
				if(idjUser.getServiceType().equals("1")){
					idjUser.setServiceType("嘉宾演出");
				}else{
					idjUser.setServiceType("驻场演出");
				}
			}
			if(idjUser.getUserType() == 1){
				idjUser.setUserTypeName("非组合");
			}else{
				idjUser.setUserTypeName("组合");
			}
			if(idjUser.getGender() == 1){
				idjUser.setGenderName("男");
			}else if(idjUser.getGender() == 2){
				idjUser.setGenderName("女");
			}else{
				idjUser.setGenderName("未知");
			}
		}
		ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("idjUser", idjUser);
        return modelAndView;
	}

	@Override
	public int setStatus(IdjUser user) {
		return mapper.setStatus(user);
	}

	@Override
	public ModelAndView selectByKeyId(String userName) {
		IdjUser user = mapper.selectByKeyId(userName);
		ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user", user);
        return modelAndView;
	}

	@Override
	public IdjUser userInfo(String userName) {
		return mapper.selectByKeyId(userName);
	}

	@Override
	public List<Long> selectUnsealUser() {
		return mapper.selectUnsealUser();
	}
}

